package org.jeecg.modules.demo.com.kunpeng.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.com.kunpeng.entity.BrandCategory;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.BrandCategoryQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.service.IBrandCategoryService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.com.kunpeng.vo.BrandCategoryQueryListVO;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 商品类别
 * @Author: jeecg-boot
 * @Date:   2023-01-12
 * @Version: V1.0
 */
@Api(tags="商品类别")
@RestController
@RequestMapping("/com.kunpeng/brandCategory")
@Slf4j
public class BrandCategoryController extends JeecgController<BrandCategory, IBrandCategoryService> {
	@Autowired
	private IBrandCategoryService brandCategoryService;
	
	/**
	 * 分页列表查询
	 *
	 * @param brandCategory
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "商品类别-分页列表查询")
	@ApiOperation(value="商品类别-分页列表查询", notes="商品类别-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BrandCategory>> queryPageList(BrandCategory brandCategory,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BrandCategory> queryWrapper = QueryGenerator.initQueryWrapper(brandCategory, req.getParameterMap());
		Page<BrandCategory> page = new Page<BrandCategory>(pageNo, pageSize);
		IPage<BrandCategory> pageList = brandCategoryService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param brandCategory
	 * @return
	 */
	@AutoLog(value = "商品类别-添加")
	@ApiOperation(value="商品类别-添加", notes="商品类别-添加")
	//@RequiresPermissions("com.kunpeng:brand_category:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BrandCategory brandCategory) {
		brandCategoryService.insert(brandCategory);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param brandCategory
	 * @return
	 */
	@AutoLog(value = "商品类别-编辑")
	@ApiOperation(value="商品类别-编辑", notes="商品类别-编辑")
	//@RequiresPermissions("com.kunpeng:brand_category:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BrandCategory brandCategory) {
		brandCategoryService.updateById(brandCategory);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品类别-通过id删除")
	@ApiOperation(value="商品类别-通过id删除", notes="商品类别-通过id删除")
	//@RequiresPermissions("com.kunpeng:brand_category:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		brandCategoryService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商品类别-批量删除")
	@ApiOperation(value="商品类别-批量删除", notes="商品类别-批量删除")
	//@RequiresPermissions("com.kunpeng:brand_category:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.brandCategoryService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "商品类别-通过id查询")
	@ApiOperation(value="商品类别-通过id查询", notes="商品类别-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BrandCategory> queryById(@RequestParam(name="id",required=true) String id) {
		BrandCategory brandCategory = brandCategoryService.getById(id);
		if(brandCategory==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(brandCategory);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param brandCategory
    */
    @RequiresPermissions("com.kunpeng:brand_category:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BrandCategory brandCategory) {
        return super.exportXls(request, brandCategory, BrandCategory.class, "商品类别");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("com.kunpeng:brand_category:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BrandCategory.class);
    }

	 /**
	  * 商品类别模糊查询
	  * @param brandCategoryQueryListDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "商品类别表-商品类别模糊查询")
	 @PostMapping(value = "/queryListByCategoryName")
	 public Result<Page<BrandCategoryQueryListVO>> queryListByCategoryName(@RequestBody BrandCategoryQueryListDTO brandCategoryQueryListDTO,
																	@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																	@RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<BrandCategoryQueryListVO>> result = new Result<Page<BrandCategoryQueryListVO>>();
		 Page<BrandCategoryQueryListVO> pageList = new Page<BrandCategoryQueryListVO>(pageNo, pageSize);
		 pageList = brandCategoryService.queryListByCategoryName(pageList, brandCategoryQueryListDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 商品类别模糊查询全部
	  * @param brandCategoryQueryListDTO
	  * @param pageNo
	  * @param pageSize
	  * @return
	  */
	 @ApiOperation(value = "商品类别表-商品类别模糊查询全部")
	 @PostMapping(value = "/queryListByCategoryNameAll")
	 public Result<Page<BrandCategoryQueryListVO>> queryListByCategoryNameAll(@RequestBody BrandCategoryQueryListDTO brandCategoryQueryListDTO,
																		   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
																		   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		 log.info("开始处理根据关键字查询项目列表请求...");
		 Result<Page<BrandCategoryQueryListVO>> result = new Result<Page<BrandCategoryQueryListVO>>();
		 Page<BrandCategoryQueryListVO> pageList = new Page<BrandCategoryQueryListVO>(pageNo, pageSize);
		 pageList = brandCategoryService.queryListByCategoryNameAll(pageList, brandCategoryQueryListDTO);//通知公告消息
		 if (pageList.getRecords().size() == 0) {
			 return Result.error("没有符合条件的数据，请重新输入！");
		 }
		 log.info("查询当前页：" + pageList.getCurrent());
		 log.info("查询当前页数量：" + pageList.getSize());
		 log.info("查询结果数量：" + pageList.getRecords().size());
		 log.info("数据总数：" + pageList.getTotal());
		 result.setSuccess(true);
		 result.setResult(pageList);
		 return Result.ok(pageList);
	 }

	 /**
	  * 启用商品类别
	  * @param id
	  * @return
	  */
	 @ApiOperation(value = "商品类别表-启用商品")
	 @GetMapping(value = "/enableCategory")
	 public Result<String> enableCategory(String id){
		 brandCategoryService.enableCategory(id);
		 return Result.ok("启用成功！");
	 }

	 /**
	  * 禁用商品类别
	  * @param id
	  * @return
	  */
	 @ApiOperation(value = "商品类别表-禁用商品")
	 @GetMapping(value = "/unEnableCategory")
	 public Result<String> unEnableCategory(String id){
		 brandCategoryService.unEnableCategory(id);
		 return Result.ok("禁用成功！");
	 }
}
